package demo8;
//循环队列
public class ArrayQueue {
    public int front;
    public int rear;
    public int[]nums;
    public ArrayQueue(int k){
        nums=new int[k+1];
    }
    //判空
   public boolean isEmpty(){

       return front==rear;
   }
   //判满
    public boolean isFull(){
        return (rear+1)% nums.length==front;
    }
    //入队列
    public void enQueue(int val){
        if(isFull()){
            System.out.println("队列满！！！");
            return;
        }
        nums[rear]=val;
        rear=(rear+1)%nums.length;
    }
    //出队列
    public int deQueue(){
        if(isEmpty()){
            System.out.println("队列为空！！！");
            return -1;
        }
        int val=nums[front];
        front=(front+1)%nums.length;//标记后移
        System.out.println("出队列成功！！！");
        return val;
    }
    //获取队头元素
    public int Front(){
        if(isEmpty()){
            System.out.println("队空！！！");
            return -1;
        }
        return nums[front];
    }
    //获取队尾元素
    public int Rear(){
        if(isEmpty()){
            System.out.println("队空！！！");
            return -1;
        }
            int index=(rear==0)?nums.length-1:rear-1;//把已经移后的指针往前移
            return nums[index];
    }
    //遍历队列中元素
    public void display(){
        for(int i=0;i<nums.length;i++){
            System.out.print(nums[i]+" ");
        }
        System.out.println();
    }
}
